using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Library.WebControls.SimplePaging {
	[SupportsEventValidation]
	[ToolboxData("<{0}:SpPaging runat=server></{0}:SpPaging>")]
	[DesignTimeVisible(true)]
	public class SpPaging : BaseControl {

		public SpPaging() {
			PageIndex = 1;
			PageSize = 10;
		}
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		[Browsable(false)]
		public override string ClientName {
			get {
				return base.ClientName;
			}
			set {
				base.ClientName = value;
			}
		}
		[Browsable(false)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public override string ID {
			get {
				return base.ID;
			}
			set {
				base.ID = value;
			}
		}

		protected override string StyleSheetFileName {
			get {
				return "sppaging-ui.css";
			}
		}

		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);
			IncludeScriptOnLoad(typeof(SpPaging), "Pg", String.Empty);
		}



		private TablePaging _pagingTable;
		[Description("Provides access to an SimpleGridView's edit template form.")]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		
		public LeftCellPaging LeftPagingControl {
			get {
				EnsureChildControls();
				return ProtectTablePaging.RowPaging.LeftCellPaging;
			}
		}
		[Description("Provides access to an SimpleGridView's edit template form.")]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public CenterCellPaging CenterPagingControl {
			get {
				EnsureChildControls();
				return ProtectTablePaging.RowPaging.CenterCellPaging;
			}
		}
		[Description("Provides access to an SimpleGridView's edit template form.")]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		[DefaultValue("")]
		public RightCellPaging RightPagingControl {
			get {
				EnsureChildControls();
				return ProtectTablePaging.RowPaging.RightCellPaging;
			}
		}
		//public Table PagingTable {
		//  get { return ProtectTablePaging; }
		//}

		internal TablePaging ProtectTablePaging {
			get {
				EnsureChildControls();
				return _pagingTable;
			}
		}

		protected override void CreateChildControls() {
			_pagingTable = new TablePaging();
			Controls.Add(_pagingTable);
		}
		[DefaultValue(1)]
		public int PageIndex { get; set; }

		private int _pageSize;
		[DefaultValue(10)]
		public int PageSize {
			get { return _pageSize; }
			set {
				if (value > 0) _pageSize = value;
			}
		}


		[DefaultValue(0)]
		[Browsable(false)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public int TotalItems { get; set; }
		//[Description("Text box for date edit")]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		
		public IconControl Refresh {
			get { return ProtectTablePaging.RowPaging.LeftCellPaging.Refresh; }
		}
		//[Description("Text box for date edit")]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		
		public IconControl Delete {
			get { return ProtectTablePaging.RowPaging.LeftCellPaging.Delete; }
		}
		//[Description("Text box for date edit")]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		
		public IconControl Edit {
			get { return ProtectTablePaging.RowPaging.LeftCellPaging.Edit; }
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public IconControl Cancel {
			get { return ProtectTablePaging.RowPaging.LeftCellPaging.Cancel; }
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public IconControl Save {
			get { return ProtectTablePaging.RowPaging.LeftCellPaging.Save; }
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public IconControl New {
			get { return ProtectTablePaging.RowPaging.LeftCellPaging.New; }
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public Image First {
			get { return ProtectTablePaging.RowPaging.CenterCellPaging.First; }
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public Image Preview {
			get { return ProtectTablePaging.RowPaging.CenterCellPaging.Preview; }
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public SpliterControl LeftSpliter {
			get { return ProtectTablePaging.RowPaging.CenterCellPaging.LeftSpliter; }
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public Label PageText {
			get { return ProtectTablePaging.RowPaging.CenterCellPaging.PageControl; }
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		internal TextBox CurrentPageContainer {
			get { return ProtectTablePaging.RowPaging.CenterCellPaging.CurrentPageText; }
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public Label LeftInText {
			get { return ProtectTablePaging.RowPaging.CenterCellPaging.LeftOfControl; }
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public Label TotalPagesText {
			get { return ProtectTablePaging.RowPaging.CenterCellPaging.TotalPagesText; }
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public SpliterControl CenterSpliter {
			get { return ProtectTablePaging.RowPaging.CenterCellPaging.RightSpliter; }
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public Image Next {
			get { return ProtectTablePaging.RowPaging.CenterCellPaging.Next; }
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]

		public Image Last {
			get { return ProtectTablePaging.RowPaging.CenterCellPaging.Last; }
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public Label RowPerPageText {
			get { return ProtectTablePaging.RowPaging.CenterCellPaging.NumberOfRowDisplayControl; }
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		internal TextBox RowPerPageContainer {
			get { return ProtectTablePaging.RowPaging.CenterCellPaging.NumberRowOfText; }
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public Label ShowText {
			get { return ProtectTablePaging.RowPaging.RightCellPaging.Show; }
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public Label StartPageText {
			get { return ProtectTablePaging.RowPaging.RightCellPaging.StartPage; }
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		
		public Label RightSpliter {
			get { return ProtectTablePaging.RowPaging.RightCellPaging.Spliter; }
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public Label EndPageText {
			get { return ProtectTablePaging.RowPaging.RightCellPaging.EndPage; }
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public Label RightInText {
			get { return ProtectTablePaging.RowPaging.RightCellPaging.Of; }
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public Label TotalItemText {
			get { return ProtectTablePaging.RowPaging.RightCellPaging.TotalIems; }
		}
		[DefaultValue(0)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		[Browsable(false)]
		public int TotalPages {
			get {
				int totalPage = 0;
				if (PageSize > 0) {
					float floatTotalPage = TotalItems / (float)PageSize;
					totalPage = (Int32)floatTotalPage;
					if (totalPage < floatTotalPage) totalPage++;

				}
				return totalPage;
			}
		}
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		[Browsable(false)]
		public override bool ClientEnable {
			get {
				return base.ClientEnable;
			}
			set {
				base.ClientEnable = value;
			}
		}
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		[Browsable(false)]
		public override bool ClientVisible {
			get {
				return base.ClientVisible;
			}
			set {
				base.ClientVisible = value;
			}
		}
		protected override void Render(HtmlTextWriter writer) {
			var basePage = Page as BasePage;
			if (basePage != null) {
				var paging = this;
				paging.Refresh.ToolTip = basePage.CulText.Refresh;
				paging.New.ToolTip = basePage.CulText.New;
				paging.Edit.ToolTip = basePage.CulText.Edit;
				paging.Save.ToolTip = basePage.CulText.Save;
				paging.Delete.ToolTip = basePage.CulText.Delete;
				paging.Next.ToolTip = basePage.CulText.NextPage;
				paging.Preview.ToolTip = basePage.CulText.PreviewPage;
				paging.First.ToolTip = basePage.CulText.FirstPage;
				paging.Last.ToolTip = basePage.CulText.LastPage;
				paging.PageText.Text = basePage.CulText.Page;
				paging.LeftInText.Text = basePage.CulText.In;
				paging.RowPerPageText.Text = basePage.CulText.RowPerPage;
				paging.ShowText.Text = basePage.CulText.View;
				paging.RightInText.Text = paging.LeftInText.Text;
			}
			CurrentPageContainer.Text = PageIndex.ToString();
			TotalPagesText.Text = TotalPages.ToString();
			RowPerPageContainer.Text = PageSize.ToString();

			if (PageIndex > 0) {
				StartPageText.Text = ((PageIndex - 1) * PageSize + 1).ToString();
				EndPageText.Text = ((PageIndex) * PageSize).ToString();
			}

			TotalItemText.Text = TotalItems.ToString();
			base.Render(writer);
		}

		internal void BuildClientFunctionForPaging(string clientFunctionGetPage, string refreshFunction) {
			if (!String.IsNullOrEmpty(clientFunctionGetPage)) {
				First.Attributes["onclick"] = String.Format(clientFunctionGetPage, 1);
				int currentPageIndex = PageIndex;
				if (currentPageIndex > 1) {
					Preview.Attributes["onclick"] = String.Format(clientFunctionGetPage, currentPageIndex - 1);
				}
				int maxPageIndex = TotalPages;

				if (currentPageIndex < maxPageIndex) {
					Next.Attributes["onclick"] = String.Format(clientFunctionGetPage, currentPageIndex + 1);
					Last.Attributes["onclick"] = String.Format(clientFunctionGetPage, maxPageIndex);
				}
			}
			if (!String.IsNullOrEmpty(refreshFunction))
				Refresh.Attributes["onclick"] = refreshFunction;

		}
	}
}
